package com.rem.concurrency.juc.my7Unsafe;

import cn.hutool.core.util.RandomUtil;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author Rem
 * @date 2021-03-08
 */

public class SetTest {

    public static void main(String[] args) {


        /**
         * HashSet 底层是HashMap  不安全
         * 代替方案：
         * Collections.synchronizedSet(new HashSet<>());
         * new CopyOnWriteArraySet<>();
         *
         */

        //  Set<String> set = new HashSet<>();
        //Set<Object> set = Collections.synchronizedSet(new HashSet<>());
        Set<String> set = new CopyOnWriteArraySet<>();


        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                set.add(RandomUtil.randomString(4));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
